/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function eliminarSoftwareP(){ 
     id = $(this).closest('tr').attr("id");
    if(!confirm("Seguro que desea eliminar el software "+ id))
        return;   
    $.ajax(
    {
        type: "GET",
        url: "eliminarSoftwareP",
        async: false,
        dataType: "text",
        data: "idSoft="+ id,
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            alert(transport); 
            $("#"+id).remove()
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function buscarSoftwareP(){
    if($("#buscar").val().length===0){
        return;
    }
    $.ajax(
    {
        type: "GET",
        url: "buscarSoftwareP",   
        async: false,
        dataType: "text",
        data: "buscar="+$("#buscar").val(),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            $("#body-content").html(transport);            
            $('input[id="btnEliminar"]').click(eliminarSoftwareP);
            $('input[id="btnModificar"]').click(modificarSoftwareP);
            $('#btnAgregar').click(agregarSoftwareP);
            $('#btnBuscar').click(buscarSoftwareP);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function modificarSoftwareAP(){
    if(!validaAltaSoftwareP()){
        alert("Campos Invalidos");
        return false;
    }
    
    $.ajax({
        type: "POST",
        url: "modificarSoftwareP",
        async: false,
        dataType: "text",
        data: $('#modficarSoftwareF').serialize(),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            alert(transport);
            $("#body-content").html("");
            $('#menuSoftware').trigger('click');
                               
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }        
    });
    
    $('#menuSoftware').trigger('click');
    $('#menuSoftware').trigger('click');
    return false;
}

function mostrarSoftwareP(){
    $("#verSoftware").hide();   
    $("#tablaSoftware").show();
}

function agregarSoftwareP(){  
    $("#tablaSoftware").hide();
    
    $.ajax(
    {
        type: "GET",
        url: "GestionSoftware/gestionAgregarSoftware.jsp",
        async: false,
        dataType: "text",
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            $("#verSoftware").html(transport);
            $(".error-inner").hide();
            $("#verSoftware").show();
            $("#tablaSoftware").hide();
            $('#btnAltaSoftware').click(altaSoftwareP);
            $("#btnRegresar").click(mostrarSoftwareP);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}


function altaSoftwareP(){
    if(!validaAltaSoftwareP())
        return false;
    console.log($('#altaSoftware').serialize());   
    $.ajax(
    {
        type: "POST",
        url: "altaSoftwareP",
        async: false,
        dataType: "text",
        data: $('#altaSoftware').serialize(),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            alert(transport);
            $("#body-content").html("");
            $('#menuSoftware').trigger('click');
                               
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
    $('#menuSoftware').trigger('click');
    $('#menuSoftware').trigger('click');
    return false;
}

function modificarSoftwareP(){    
    $("#tablaSoftware").hide();
    
    $.ajax(
    {
        type: "GET",
        url: "gestionModificarSoftwarePreP",
        async: false,
        dataType: "text",
        data: "idSoft="+$(this).closest('tr').attr("id"),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            $("#verSoftware").html(transport);
            $(".error-inner").hide();
            $("#verSoftware").show();
            $("#tablaSoftware").hide();
            $('#btnModificarSoftware').click(modificarSoftwareAP);
            $("#btnRegresar").click(mostrarSoftwareP);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}


function validaAltaSoftwareP(){
    $('.error-inner').hide();
    esValido = true;
//    if($('#idSoft').val().length<3){
//        $("#eidSoft").show();
//        esValido = false;
//    }
    if($('#soft').val().length<3){
        $("#esoft").show();
        esValido = false;
    }
    if($('#descripcion').val().length<3){
        $("#edescripcion").show();
        esValido = false;
    }
    return esValido;
}
